package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.SimplePerson;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Comparator;

/**
 * @date: 2022-08-02 20:23
 * @author: ztkj-hzb
 * @description: 排序设置
 **/
public class TableViewSortSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();

        HBox hBox = new HBox();
        Button ascButton = new Button("升序");
        Button descButton = new Button("降序");
        hBox.getChildren().addAll(ascButton, descButton);
        hBox.setSpacing(30);
        hBox.setAlignment(Pos.CENTER);

        ObservableList<SimplePerson> persons = FXCollections.observableArrayList();
        persons.add(new SimplePerson("小天", 20, "湖北省", false));
        persons.add(new SimplePerson("小地", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄", 40, "广东省", false));
        persons.add(new SimplePerson("小黄", 50, "浙江省", true));
        persons.add(new SimplePerson("小天-1", 20, "湖北省", false));
        persons.add(new SimplePerson("小地-1", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄-1", 40, "广东省", false));
        persons.add(new SimplePerson("小黄-1", 50, "浙江省", true));
        persons.add(new SimplePerson("小天-2", 20, "湖北省", false));
        persons.add(new SimplePerson("小地-2", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄-2", 40, "广东省", false));
        persons.add(new SimplePerson("小黄-2", 50, "浙江省", true));

        TableView<SimplePerson> tableView = new TableView<>(persons);

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        tableView.setPrefSize(400, 400);


        TableColumn<SimplePerson, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<SimplePerson, Number> ageTableCol = new TableColumn<>("年龄");
        TableColumn<SimplePerson, String> addressTableCol = new TableColumn<>("地址");
        TableColumn<SimplePerson, Boolean> marriedTableCol = new TableColumn<>("是否已婚");

        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        ageTableCol.setCellValueFactory(new PropertyValueFactory<>("age"));
        addressTableCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        marriedTableCol.setCellValueFactory(new PropertyValueFactory<>("married"));

        tableView.getColumns().addAll(nameTableCol, ageTableCol, addressTableCol, marriedTableCol);

        anchorPane.getChildren().addAll(hBox, tableView);
        AnchorPane.setTopAnchor(tableView, 100.0);
        AnchorPane.setTopAnchor(hBox, 30.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(700);
        primaryStage.show();


        //设置是否开启排序功能，默认为true：开启
        //nameTableCol.setSortable(false);

        //设置根据指定字段排序
        //tableView.getSortOrder().addAll(nameTableCol, ageTableCol);

        //设置排序策略
        /*tableView.setSortPolicy(param -> {
            //这里以age来演示
            for (TableColumn<SimplePerson, ?> paramColumn : param.getColumns()) {
                if ("年龄".equals(paramColumn.getText())) {
                    if (paramColumn.getSortType() == TableColumn.SortType.ASCENDING) {

                        //TODO 替换箭头图标
                        paramColumn.setSortNode(new Label("升"));

                        //年龄-升序
                        tableView.getItems().sort(Comparator.comparing(SimplePerson::getAge));
                    } else if (paramColumn.getSortType() == TableColumn.SortType.DESCENDING) {
                        paramColumn.setSortNode(new Label("降"));

                        //降序
                        tableView.getItems().sort((o1, o2) -> o2.getAge().compareTo(o1.getAge()));
                    }
                    return true;
                }
            }
            return false;
        });*/


        ascButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //设置指定字段排序
                nameTableCol.setSortType(TableColumn.SortType.ASCENDING);
            }
        });

        descButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                nameTableCol.setSortType(TableColumn.SortType.DESCENDING);
            }
        });

    }
}
